package com.aizuda.boot.modules.business.longterm.controller;

import com.aizuda.boot.modules.business.contract.dto.SingleResponse;
import com.aizuda.boot.modules.business.longterm.domain.response.LongTermDetailVO;
import com.aizuda.boot.modules.business.longterm.domain.response.LongTermVo;
import com.aizuda.boot.modules.business.longterm.domain.resquest.LongTermSaveDTO;
import com.aizuda.boot.modules.business.longterm.domain.resquest.LongTermUpdateDTO;
import com.aizuda.boot.modules.business.longterm.service.LongTermContractService;
import com.aizuda.boot.modules.system.entity.SysDict;
import com.aizuda.boot.modules.system.service.ISysDictService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author sangyirong
 * @ClassName LongTermController
 * @Version 1.0
 * @Date 2025/7/7 16:07
 */
@RestController
@RequestMapping("/longTerm")
@Tag(name = "长期合同管理", description = "长期合同管理")
public class LongTermController {

    @Resource
    private LongTermContractService longTermContractService;

    @Resource
    private ISysDictService sysDictService;

    private static final String dictCode = "longterm";


    @Operation(summary = "获取长期合同状态")
    @GetMapping("/getAutoNumber")
    public SingleResponse<Integer> getBackStatus() {
        SysDict auto = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery()
                .eq(SysDict::getCode, dictCode)
                .last("limit 1"));
        return SingleResponse.of(auto.getStatus());
    }

    @Operation(summary = "修改长期合同状态")
    @PutMapping("/updateAutoNumber")
    public SingleResponse updateBackStatus() {
        SysDict auto = sysDictService.getOne(Wrappers.<SysDict>lambdaQuery()
                .eq(SysDict::getCode, dictCode)
                .last("limit 1"));
        auto.setStatus(auto.getStatus() == 1 ? 0 : 1);
        sysDictService.updateById(auto);
        return SingleResponse.success();
    }

    @Operation(summary = "新增长期合同管理")
    @PostMapping("/save")
    public boolean save(@RequestBody LongTermSaveDTO dto) {
        return longTermContractService.saveLongTermContract(dto);
    }

    @Operation(summary = "查询长期合同")
    @GetMapping("/query/{id}")
    public LongTermDetailVO query(@PathVariable Long id) {
        return longTermContractService.queryDetail(id);
    }

    @Operation(summary = "查询长期合同列表")
    @GetMapping("/list")
    public List<LongTermVo> list() {
        return longTermContractService.listVO();
    }

    @Operation(summary = "修改长期合同")
    @PutMapping("/update")
    public boolean update(@RequestBody LongTermUpdateDTO dto) {
        return longTermContractService.updateLongTermContract(dto);
    }

    @Operation(summary = "删除长期合同")
    @DeleteMapping("/remove/{id}")
    public boolean remove(@PathVariable Long id) {
        return longTermContractService.removeLongTermContract(id);
    }
}
